<?php
class CartAdminForm extends Form
{
	private $cart = array(), $cart_items = array(), $items = array();
	
	function CartAdminForm()
	{
		Form::Form('CartAdminForm');
		
		CGlobal::$website_title = "Quản lý đơn hàng";
		
		$this->link_css("style/aznet/ui.datepicker.css");
		$this->link_js("javascript/jquery/ui.datepicker.js");
	}
	
	function draw()
	{
		global $display;
		
		$display->add('msg',$this->showFormErrorMessages(1));
		
		
		$display->add("url_admin",Url::build_current());
		
		$status_select	=	Url::get('status_select',6);
		
		//1:Chưa cập nhật; 2: Đã gửi đơn hàng ; 3: Đã TT; 4: Hủy đơn hàng; 5: Đã duyệt
		$status_arr	=	array(
								6	=>	"--Tất cả--",
								2	=>	"Đã gửi đơn hàng",
								1	=>	"Chưa cập nhật",
								3	=>	"Đã Thanh toán",
								5	=>	"Đã duyệt",
								4	=>	"Đã hủy"
								);
		
		$display->add("status_option",AZLib::getOption($status_arr,$status_select));
		
		
		
		$date_begin		=	Url::get('date_begin');
		$date_end		=	Url::get('date_end');
		
		
		$display->add("date_begin",$date_begin);
		$display->add("date_end",$date_end);
		
		$created_time_from	= 0;
		$created_time_to	= 0;
		
		if($date_begin)
		{
			$date_arr = explode("-",$date_begin);
			if(isset($date_arr[0]) && isset($date_arr[1]) && isset($date_arr[2]))
			$created_time_from = mktime(0,0,0,(int)$date_arr[1],(int)$date_arr[0],(int)$date_arr[2]);
		}
		
		if($date_end)
		{
			$date_arr = explode("-",$date_end);
			if(isset($date_arr[0]) && isset($date_arr[1]) && isset($date_arr[2]))
			$created_time_to = mktime(23,59,59,(int)$date_arr[1],(int)$date_arr[0],(int)$date_arr[2]);
		}
		
		$condition = "";
		
		if($status_select != 6)
		{
			$condition.="status=".$status_select;
		}
		
		if ($created_time_from)
		{
			$condition.= ($condition!='' ? ' AND ' : '') .' time_create >='.$created_time_from;
		}
		
		if ($date_end)
		{
			$condition.= ($condition!='' ? ' AND ' : '') ." time_create <=$created_time_to";
		}
		
		$total = DB::count("cart",$condition);
		
		$limit = '';
		
		$display->add('paging',AZPagging::paging_list($limit,$total,40,10,'page_no','',true,'Đơn hàng'));
		
		$all_carts = array();
		
		$re = DB::query("SELECT * FROM cart ".($condition!='' ? 'WHERE ' : '') . $condition." ORDER BY id DESC $limit");	
		
		if($re)
		{
			while($Cart = mysql_fetch_assoc($re))
			{
				$Cart["time_create"] 	= date('d/m/Y H:i:s',$Cart['time_create']);
				$Cart["time_change"] 	= date('d/m/Y H:i:s',$Cart['time_change']);
				$Cart["url"] 			= Url::build_current(array('cmd'=>'edit','id'=>$Cart['id']));
				
				$Cart["status"] 		= $status_arr[$Cart['status']];
				$Cart["delete"] 		= Url::build_current(array('cmd'=>'delete','id'=>$Cart['id']));
				
				$Cart["price"] 		= number_format($Cart['price'],null,null,'.');
				
				$all_carts[$Cart['id']] = $Cart;
			}
		}
		
		$display->add('all_carts',$all_carts);
		$display->add('is_admin', User::is_admin());
		
		$this->beginForm();
		$display->output('CartAdmin');
		$this->endForm();
	}
}
?>